using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using FluffyUnderware.DevTools.Extensions;
using UnityEngine.UI;
using Yoozoo.UI.YGUI;

namespace com.yoozoo.gta.Art.BeautyGirl
{
    [System.Serializable]
    public class HairMaterials
    {
        public List<Material> hairMaterials= new List<Material>();
    }

    public class ChangeMaterialTool : MonoBehaviour
    {
        [SerializeField]private List<SkinnedMeshRenderer> skinnedMeshRenderer ;
        [Header("发色更换按钮父节点")][SerializeField] public RectTransform rectTransform;
        [Header("发色更换按钮预制体")][SerializeField] public GameObject materialMenuOptions;
        [Header("每个发型对应的所有材质列表")][SerializeField] public List<HairMaterials> _hairMaterials = new List<HairMaterials>();
        [SerializeField]private List<YButton> buttons = new List<YButton>();

        private List<GameObject> _allInstButton = new List<GameObject>(); // 当前所有被实例化出来的button预制体
        
        ChangeHairTool _changeHairTool;
        
        private void Start()
        {
            _changeHairTool = GetComponent<ChangeHairTool>();
            skinnedMeshRenderer = new List<SkinnedMeshRenderer>();
            for (int i = 0; i < _changeHairTool.hairList.Length; i++)
            {
                skinnedMeshRenderer.Add(_changeHairTool.hairList[i].GetComponent<SkinnedMeshRenderer>());
            }
        }
        public void OnChangeMaterial()
        {
            Init(_changeHairTool.currIdx);
        }

        public void Init(int idx)
        {
            ClearData();
            
            if (skinnedMeshRenderer == null || _hairMaterials == null || _hairMaterials.Count == 0)
            {
                Debug.LogError("ChangeMaterialTool: ChangeMaterial() skinnedMeshRenderer or hairMaterials is null or empty");
                return;
            }
            
            int hairMaterialsCount = _hairMaterials[idx].hairMaterials.Count;
            int hairMaterialGroups = hairMaterialsCount / 2;
            //取余
            int hairMaterialRemainder = hairMaterialsCount % 2;
            
            for (int i = 0; i < hairMaterialGroups; i++)
            {
                GameObject instHairMaterialGroup = Instantiate(materialMenuOptions, rectTransform);
                buttons.AddRange(instHairMaterialGroup.GetComponentsInChildren<YButton>());
                _allInstButton.Add(instHairMaterialGroup);
            }
            if(hairMaterialRemainder > 0)
            {
                GameObject instHairMaterialGroup = Instantiate(materialMenuOptions, rectTransform);
                buttons.Add(instHairMaterialGroup.GetComponentsInChildren<YButton>()[0]);
                _allInstButton.Add(instHairMaterialGroup);
                Destroy(instHairMaterialGroup.GetComponentsInChildren<YButton>()[1].transform.parent.gameObject);
            }
            
            for (int i = 0; i < buttons.Count; i++)
            {
                int index = i;
                buttons[i].onClick.AddListener(() => OnButtonClicked(index));
                buttons[i].GetComponentInChildren<Text>().text = "发色 " + i;
                buttons[i].GetComponentInChildren<Text>().name = "发色 " + i;
            }
        }

        private void OnButtonClicked(int index)
        {
            int idx = _changeHairTool.currIdx;
            Debug.Log("ChangeMaterialTool: OnButtonClicked() index: " + index);
            skinnedMeshRenderer[idx].sharedMaterial = _hairMaterials[idx].hairMaterials[index];
        }

        private void ClearData()
        {
            if (_allInstButton != null || _allInstButton.Count > 0)
            {
                for (int i = 0; i < _allInstButton.Count; i++)
                {
                    Destroy(_allInstButton[i]);
                }
            }

            if (buttons != null || buttons.Count > 0)
            {
                buttons.Clear();
            }
        }
    }
}